<?php

namespace app\common\model;

use think\Model;
use Carbon\Carbon;
use think\Hook;
use think\Exception;
use app\common\traits\model\TraitModel;

class QuitDeposit extends Model
{
	use TraitModel;
	
	protected $table              = 'quit_deposit';
	protected $autoWriteTimestamp = 'timestamp';
    protected $createTime         = 'created_at';
    protected $updateTime         = 'updated_at';

	const ORDER_REFUND_ON = 102; //退款中
 	const ORDER_REFUND_FINISH = -2;//退款完成

    public function createQuitDeposit($number, int $deposit, $describe = null)
    {
    	self::create(['number' => $number, 'deposit' => $deposit, 'describe' => $describe, 'status' => 102], true);
    	//后置操作
    	Hook::exec('app\\common\\behavior\\Refund','afterCreateRefund', $number);
    }

    /**
     * 设置流水状态
     * @param [type] $number [description]
     * @param [type] $status [description]
     */
    protected function setQuitDepositStatus($number, $status)
    {
    	if ($status == self::ORDER_REFUND_ON || $status == self::ORDER_REFUND_FINISH) {
    		return $this->update(['status' => $status], ['number' => $number], true);
    	}
    	throw new Exception("Invalid state value", 1);
    }

    public function delQuitDeposit($number)
    {
    	$this->destroy(['number' => $number, 'status' => self::ORDER_REFUND_FINISH]);
    }

    /**
     * 完成退款
     * @param  [type] $number [description]
     * @return [type]         [description]
     */
    public function completeRefund($number)
    {
    	$this->setQuitDepositStatus($number, -2);
        //后置操作
        Hook::exec('app\\common\\behavior\\Refund','afterCompleteRefund', $number);
    }
}